//
//  GXCycleImages.m
//  16-UICollectionViewCycleImage
//
//  Created by 曹曹 on 16/8/4.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "GXCycleImages.h"

#define kCollectionViewCellReuse @"cellReuse"
#define kArc4RandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]

@interface GXCycleImages () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@end

@implementation GXCycleImages

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setUp];
    }
    return self;
}
// SB中创建控件时走下边的方法(读取本地SB,xib等文件)
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self setUp];
        NSLog(@"%lf", self.frame.size.width);/**< 0 */
    }
    return self;
}

- (void)setUp {
    [self createCollectionView];
    [self createPageControl];
}

- (void)createCollectionView {
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
    _collectionView.pagingEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self addSubview:_collectionView];
    [_collectionView registerClass:[GXCycleImagesCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
}

- (void)createPageControl {
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _imagesArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GXCycleImagesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:_imagesArray[indexPath.row]];
    // label赋值
    // 点击方法
    // nstimer
    // 定制
    return cell;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"%lf", self.frame.size.width);/**< 正常宽度 */
    _collectionView.frame = self.bounds;
    _flowLayout.itemSize = self.bounds.size;
    [_collectionView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 直接判断与0相等有何问题
    if (scrollView.contentOffset.x <= 1) {
//        [scrollView setContentOffset:CGPointMake(_collectionView.bounds.size.width * (_imagesArray.count - 2), 0)];
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_imagesArray.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    } else if (scrollView.contentOffset.x >= _collectionView.bounds.size.width * (_imagesArray.count - 1) - 1) {
//        [scrollView setContentOffset:CGPointMake(_collectionView.bounds.size.width, 0)];
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
}

- (void)setImagesArray:(NSMutableArray *)imagesArray {
    _imagesArray = imagesArray;
    [_collectionView reloadData];
}

@end

/**
 GXCycleImagesCell
 */
@implementation GXCycleImagesCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_imageView];
    }
    return self;
}

@end
